package com.ll.agent.ASMStudy.doThrow;

import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.commons.AdviceAdapter;

/**
 * @description:
 * @author: LiuLei
 * @create: 2024-07-19
 **/
public class ThrowExceptionAdviceAdapter extends AdviceAdapter {
    protected ThrowExceptionAdviceAdapter(int api, MethodVisitor methodVisitor, int access, String name, String descriptor) {
        super(api, methodVisitor, access, name, descriptor);
    }

    @Override
    protected void onMethodEnter() {
        // 在方法开始处插入抛出异常的字节码
        mv.visitTypeInsn(NEW, "java/lang/RuntimeException");
        mv.visitInsn(DUP);
        mv.visitLdcInsn("Simulated Exception");
        mv.visitMethodInsn(INVOKESPECIAL, "java/lang/RuntimeException", "<init>", "(Ljava/lang/String;)V", false);
        mv.visitInsn(ATHROW);
    }
}
